package com.mairuide.agency.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.mairuide.agency.dao.AgencyDao;
import com.mairuide.agency.pojo.Agency;
import com.mairuide.bill.DealFlowService;
import com.mairuide.casemanage.service.CaseManageService;

/**
 * @Description
 * @auther 刘中华
 * @create 2019-01-15 11:55
 */
@Service
public class AgencyService {
    @Autowired
    private AgencyDao agencyDao;

    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    @Autowired
    private CaseManageService caseManageService;
    /**
     * @Author 刘中华
     * @Date 2019/1/15 14:17
     * @Param [agency]
     * @return void
     * @Description  保存待办对象
     **/
    public void save(Agency agency){
        agencyDao.save(agency);
    }
    
    /**
     * @Author 刘中华
     * @Date 2019/1/15 14:28
     * @Param [whereMap]
     * @return boolean
     * @Description 判断是否加入了待办列表
     **/
    public boolean isAddAgencyList(Map whereMap){

        StringBuffer sb=new StringBuffer();
        sb.append(" SELECT * FROM TB_BILL_AGENCY  WHERE 1=1 ");
        if (whereMap.get("CASEMANAGE_ID")!=null){
            sb.append(" AND CASEMANAGE_ID = '" + whereMap.get("CASEMANAGE_ID")+"'");
        }
        if (whereMap.get("ACCOUNT_BODY_CARD")!=null){
            sb.append(" AND ACCOUNT_BODY_CARD = " + whereMap.get("ACCOUNT_BODY_CARD"));
        }
        if (whereMap.get("ACCOUNT_RIVAL_CARD")!=null){
            sb.append(" AND ACCOUNT_RIVAL_CARD = '" + whereMap.get("ACCOUNT_RIVAL_CARD")+"'");
        }
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sb.toString());
        return maps.size()>0;

    }
    
    /**
     * 通过查询条件查询所有代办账号
     * @param whereMap
     * @return
     */
    public List<Map<String, Object>> getAgency4WhereMap(Map<String,Object> whereMap,Integer createId){
    	List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
        StringBuffer sb = new StringBuffer();
        sb.append(" SELECT * FROM TB_BILL_AGENCY  WHERE 1=1 ");
        if (whereMap.get("CASEMANAGE_ID")!=null){
            sb.append(" AND CASEMANAGE_ID = '" + whereMap.get("CASEMANAGE_ID")+"'");
        }
        if (whereMap.get("ACCOUNT_BODY_CARD")!=null){
            sb.append(" AND ACCOUNT_BODY_CARD = " + whereMap.get("ACCOUNT_BODY_CARD"));
        }
        if (whereMap.get("STATE")!=null){
            sb.append(" AND STATE = '" + whereMap.get("STATE")+"'");
        }
        maps = jdbcTemplate.queryForList(sb.toString());
        if(null != maps && maps.size() > 0) {
        	for(Map<String, Object> m : maps) {
        		String whichTable = caseManageService.getWhichTable(whereMap.get("CASEMANAGE_ID").toString());
        		StringBuffer sql = new StringBuffer();
        		sql.append("select * from "+whichTable+" t where t.ACCOUNT_RIVAL_CARD = '"+m.get("ACCOUNT_RIVAL_CARD")+"' ");
        		sql.append(" and t.CASEMANAGE_ID = '"+whereMap.get("CASEMANAGE_ID")+"' ");
        		List<Map<String, Object>> maps2 = jdbcTemplate.queryForList(sql.toString());
        		if(null == null || maps2.size() <= 0) {
        			return maps;
        		}
        	}
        }
        return null;

    }
}
